<?php

/**
 * @author Martin Pernica
 * @copyright 2009
 */

class Ftp
{
    public $ftp_server;
    public $ftp_user_name;
    public $ftp_user_pass;
    public $dst_dir;
    public $conn_id;
    public $login_result;
    public $ssl;

    public function __construct($ftp_server, $ftp_user_name, $ftp_user_pass, $dst_dir,
        $ssl = false)
    {
        if ($ftp_server != "" && $ftp_user_name != "" && $ftp_user_pass != "" && $dst_dir !=
            "") {
            $this->ftp_server = $ftp_server;
            $this->ftp_user_name = $ftp_user_name;
            $this->ftp_user_pass = $ftp_user_pass;
            $this->dst_dir = $dst_dir;
        } else {
            return false;
        }
        if (!$this->connect() || !$this->setdir()) {
            return false;
        } else {
            return true;
        }
    }

    public function connect()
    {
        $this->conn_id = ($this->ssl) ? @ftp_ssl_connect($this->ftp_server) : @
            ftp_connect($this->ftp_server);
        $this->login_result = @ftp_login($this->conn_id, $this->ftp_user_name, $this->
            ftp_user_pass);
        if ((!$this->conn_id) || (!$this->login_result)) {
            return false;
        } else {
            return true;
        }
    }

    public function setdir()
    {
        if (!@ftp_chdir($this->conn_id, $this->dst_dir)) {
            return false;
        } else {
            return true;
        }
    }

    public function sendfile($remote_file, $file, $mode = "FTP_BINARY")
    {
        if (@ftp_put($this->conn_id, $remote_file, $file, $mode)) {
            return true;
        } else {
            return false;
        }
    }

    public function createDir($dirName)
    {
        if (@ftp_mkdir($this->conn_id, $dirName)) {
            return true;
        } else {
            return false;
        }
    }

    public function deleteFile($file)
    {
        if (@ftp_delete($this->conn_id, ftp_pwd($this->conn_id) . '/' . $file)) {
            return true;
        } else {
            return false;
        }
    }

    public function deleteDir($dirName)
    {
        if (@ftp_rmdir($this->conn_id, $dirName)) {
            return true;
        } else {
            return false;
        }
    }

    public function close()
    {
        ftp_close($this->conn_id);
    }

    public function __destruct()
    {
        ftp_close($this->conn_id);
    }

}
?>